package com.woniu.p2p.behindController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.RiskService;
import com.woniu.p2p.entity.Invest;
import com.woniu.p2p.entity.InvestRisk;
import com.woniu.p2p.vo.InvestRiskAndInvestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@Controller
@RequestMapping("/myrisk")
public class RiskController {

    @Autowired
    RiskService riskService;


    @RequestMapping("/getAllRisk")
    public String findAllRisk(Map map, String riskstatus,
                              @DateTimeFormat(pattern = "yyyy-MM-dd") Date predate,
                              @DateTimeFormat(pattern = "yyyy-MM-dd") Date nextdate,
                              String riskkeyword,
                              Integer pagenum,
                              Integer pagesize){
        pagenum=pagenum==null?1:pagenum;
        pagesize=pagesize==null?3:pagesize;
        PageInfo<InvestRiskAndInvestVo> pageInfo = riskService.findAllInvestRiskAndInvest(pagenum, pagesize, riskstatus, predate, nextdate, riskkeyword);
        map.put("allrisk",pageInfo);
        map.put("riskstatus",riskstatus);
        map.put("predate",predate);
        map.put("nextdate",nextdate);
        map.put("riskkeyword",riskkeyword);
        map.put("pagesize",pagesize);
        List<InvestRiskAndInvestVo> list1 = pageInfo.getList();
        List<InvestRisk> list = riskService.selectAll();
        ArrayList<Integer> ids = new ArrayList<>();
        for(InvestRisk investRisk :list){
            ids.add(investRisk.getRiskId());
        }
        map.put("ids",ids);

        List<Invest> allInvest = riskService.findAllInvest();
        map.put("insertInvest",allInvest);

        return "content/riskManager";
    }


    /**
     * 导出
     */
    @RequestMapping("/outRisk")
    public void outRisk(int[] id,
                                 HttpServletResponse response) throws IOException {
        System.out.println(Arrays.toString(id));
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试","UTF-8");
        response.setHeader("Content-Dispostion","attachment;filename*=UTF-8''"+fileName+".xlsx");


        List<InvestRiskAndInvestVo> list = riskService.findAllInvestAndRisk(id);
        ExcelWriterBuilder write = EasyExcel.write(outputStream, InvestRiskAndInvestVo.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(list);
        outputStream.close();
    }


    @RequestMapping("/delonerisk")
    public int deloneafterloans(int id){
        riskService.deleteRiskById(id);
        return 1;
    }


    @RequestMapping("/delallrisk")
    public int deloneafterloans(int[] id){
        for (int i = 0; i <id.length ; i++) {
            riskService.deleteRiskById(id[i]);
        }
        return 1;
    }


    @RequestMapping("/getriskdetail")
    @ResponseBody
    public Invest getriskdetail(int id){
        Invest allInvestById = riskService.findAllInvestById(id);
        return allInvestById;
    }


    @RequestMapping("/insertRisk")
    public int insertAfterloans(String riskBasename,String riskCainame,String riskJingname,String riskChangname,
                                String riskShiname,int riskPingname,String riskDengname,String riskDanname,String riskDiname,
                                String riskJigouname
    ){
        //先修改

        //在增加
        InvestRisk investRisk = new InvestRisk( riskBasename,  riskCainame,  riskJingname,  riskChangname,  riskShiname,  riskDanname,  riskDiname,  riskJigouname,  riskPingname, riskDengname);
        riskService.insertRisk(investRisk);
        return 1;
    }







}
